//
//  ConfigTableViewController.m
//  TutorialsModule
//
//  Created by Luciano Perez Rocha on 03/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ConfigTableViewController.h"
#import "Languajes.h"
#import "Aboutus.h"
#import "Accounts.h"

@implementation ConfigTableViewController

@synthesize itmesConfig;
@synthesize itemsAboutus;

-(id) initWithNibName: (NSString*) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
	
	if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
	{
		itmesConfig = [[NSMutableArray alloc] init];  
		itemsAboutus = [[NSMutableArray alloc] init];
		//Creo los tutoriales que van a controlar las vistas que cada celda abre
		
		Accounts *accountsController = [[Accounts alloc] initWithNibName:@"Accounts" bundle:nil];
		Aboutus *aboutController = [[Aboutus alloc] initWithNibName:@"Aboutus" bundle:nil];
		Languajes *languajesController = [[Languajes alloc] initWithNibName:@"Languajes" bundle:nil];
		
		//genero el array con los textos de las celdas y los controladores.
		
		[itemsAboutus addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"About Tutorials",@"texto_celda",aboutController,@"controller",nil]];
		
		[itmesConfig addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Languaje",@"texto_celda",languajesController,@"controller",nil]];
		[itmesConfig addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Accounts",@"texto_celda",accountsController,@"controller",nil]];
		
				
		[accountsController release];
		[languajesController release];
		[aboutController release];
		
	}
	return self;
	
}

/*- (void)awakeFromNib {  
	// we'll keep track of our views in this array  
} */ 


/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
}*/


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0) return [itemsAboutus count];
	else return [itmesConfig count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	
	if(indexPath.section > 0) cell.textLabel.text= [[itmesConfig objectAtIndex:indexPath.row] objectForKey:@"texto_celda"];
	else cell.textLabel.text= [[itemsAboutus objectAtIndex:indexPath.row] objectForKey:@"texto_celda"];
		
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	
	//obtengo el controlador asociado a la celda que hizo click.
	UIViewController *targetViewController;
	
	if(indexPath.section > 0) 
	{
		targetViewController = [[itmesConfig objectAtIndex: indexPath.row] objectForKey:@"controller"];  
		targetViewController.title = [[itmesConfig objectAtIndex: indexPath.row] objectForKey:@"texto_celda"];
		[self.navigationController pushViewController:targetViewController animated:YES]; 
	}
	else
	{
		targetViewController = [[itemsAboutus objectAtIndex: indexPath.row] objectForKey:@"controller"];  
		targetViewController.title = [[itemsAboutus objectAtIndex: indexPath.row] objectForKey:@"texto_celda"];
		[self.navigationController pushViewController:targetViewController animated:YES]; 
	
	}
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
	[itmesConfig release];
}


@end

